<%@page import="fi.foyt.cs.persistence.domainmodel.scene.SceneType"%>
<%@page import="fi.foyt.cs.persistence.domainmodel.scene.SceneCategory"%>
<%@page import="fi.foyt.cs.persistence.domainmodel.scene.SceneCategoryType"%>
<%@page import="fi.foyt.foursquare.api.entities.Category"%>
<%@page import="fi.foyt.cs.foursquare.FoursquareUtils"%>
<%@page import="fi.foyt.foursquare.api.FoursquareApi"%>
<%@page import="fi.foyt.cs.controller.SceneController"%>
<%@page import="fi.foyt.cs.persistence.domainmodel.task.GeneralTask"%>
<%@page import="fi.foyt.cs.controller.TaskController"%>
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt"%>
<%@taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn"%>
<%@ page import="fi.foyt.cs.persistence.domainmodel.task.TaskType" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
  <jsp:include page="../fragments/head.jsp"></jsp:include>
</head>
<body>
  <jsp:include page="../fragments/header.jsp"></jsp:include>
  
  <h1>
    Foursquare Category Import
  </h1>
  
  <%
    FoursquareApi foursquareApi = FoursquareUtils.getFoursquareApi();
    SceneController sceneController = new SceneController();
    Category[] categories = foursquareApi.venuesCategories();
    
    for (Category rootCategory : categories) {
      for (Category subCategory : rootCategory.getCategories()) {
        SceneCategory sceneCategory = sceneController.findSceneCategoryByTypeAndExternalId(SceneCategoryType.Foursquare,  subCategory.getId());
      	if (sceneCategory == null) {
      	  sceneCategory = sceneController.createSceneCategory(null, subCategory.getName(), SceneCategoryType.Foursquare, subCategory.getId());
      	  out.append("<div>Added category " + sceneCategory.getName() + "</div>");
      	} else {
      	  sceneController.updateSceneCategoryName(sceneCategory, subCategory.getName());
      	  out.append("<div>Updated category " + sceneCategory.getName() + "</div>");
      	}
      	
      	if (subCategory.getCategories() != null) {
      	  for (Category subSubCategory : subCategory.getCategories()) {
      	    SceneCategory subSceneCategory = sceneController.findSceneCategoryByTypeAndExternalId(SceneCategoryType.Foursquare,  subSubCategory.getId());
          	if (subSceneCategory == null) {
          	  subSceneCategory = sceneController.createSceneCategory(sceneCategory, subSubCategory.getName(), SceneCategoryType.Foursquare, subSubCategory.getId());
          	  out.append("<div>Added subcategory " + subSceneCategory.getName() + "</div>");
          	} else {
          	  sceneController.updateSceneCategoryName(subSceneCategory, subSubCategory.getName());
          	  out.append("<div>Updated subcategory " + subSceneCategory.getName() + "</div>");
          	}  
      	  }	  
      	}
      }
      
    }
  %>
  
  <jsp:include page="../fragments/footer.jsp"></jsp:include>
</body>
</html>